import { SetStateAction, useRef, useState } from 'react'
import { postCompanyInfoDetailSoftwarePageList } from '@/api/companyDetail/intellectual-property'
import { useNewPage } from '@hs-admin/utils'
import { HsAdminTablePage } from '@hs-admin/base-components'
import { DetailModal } from '@hs-server/components'

const SoftwareWritings = (props: any) => {
  const { companyCode, hiddenModule } = props
  const columns = [
    {
      title: '序号',
      dataIndex: 'index',
      width: 80,
      render: (_: string, _record: any, index: number) => <>{index + 1 + (pagination.current - 1) * pagination.pageSize}</>
    },
    {
      title: '软件名称',
      dataIndex: 'swFname',
      render: (text: string, record: any) => (
        <span
          className="link cursor-pointer"
          onClick={() => {
            record.eventSubType = '软件著作详情'
            DetailModalRef.current.showModal(record)
          }}>
          {text}
        </span>
      )
    },
    {
      title: '软件简称',
      dataIndex: 'swSname'
    },
    {
      title: '版本号',
      dataIndex: 'version'
    },
    {
      title: '首次发布日期',
      dataIndex: 'firstPubDate'
    },
    {
      title: '登记号',
      dataIndex: 'regNo'
    },
    {
      title: '登记批准日期',
      dataIndex: 'approveDate'
    }
  ]
  const { pagination } = useNewPage(callList)
  const [data, setdata] = useState([])
  const [iscount, setiscount] = useState('')
  const DetailModalRef = useRef<any>({})
  const [isShow, setIsShow] = useState(false)

  function callList() {
    return postCompanyInfoDetailSoftwarePageList({
      pageNo: pagination.current,
      pageSize: pagination.pageSize,
      companyCode: companyCode || 'd79cfd72313f9a282a60afb3d85752c9'
    }).then((res: { data: { list: SetStateAction<never[]>; total: any } }) => {
      setdata(res.data.list)
      setIsShow(res.data.total ? true : false)
      setiscount(res.data.total ?? '')
      hiddenModule(res.data.total ?? '') //总条数 传给父组件
      return res.data
    })
  }

  return (
    <>
      {isShow && (
        <div className="intellectual-property-softwareWritings-wrapper" id="软件著作" data-count={iscount}>
          <h3 className="mb-4">软件著作</h3>
          <HsAdminTablePage
            columns={columns}
            dataSource={data}
            id="intellectual-property-softwareWritings"
            pagination={pagination}
            rowKey={(record: { id: string }) => record.id}
            size="small"
          />
          <DetailModal ref={DetailModalRef} />
        </div>
      )}
    </>
  )
}

export default SoftwareWritings
